const db = require('../utils/db.js');

// 操作数据库
function handleDB(res,tbName,mdName,errmsg,n1,n2){
    let results;
    let Model = db.model(tbName);
    try{
        results = new Promise((resolve,reject)=>{
            if(!n1){
                Model[mdName]((err,data)=>{
                    if(err)reject(err);
                    resolve(data);
                });
                return   
            }
            if(!n2){
                Model[mdName](n1,(err,data)=>{
                    if(err)reject(err);
                    resolve(data);
                }); 
                return   
            }
            Model[mdName](n1, n2,(err,data)=>{
                if(err)reject(err);
                resolve(data);
            }); 
          
              
        })
    }catch(err){
        res.json({errmsg:errmsg});
        return
    }
    return results
}
module.exports = handleDB